using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace FederationMembershipDB
{
	public class TrainingPass
	{
		string _payeeName;
		PassType _passType;
		DateTime _issueDate;
		double _feePaid;
		string _issuedBy;
		int _numberOfClassesRemaining;
        List<Member> _members = new List<Member>();

        public string PayeeName
        {
            get { return _payeeName; }
            set { _payeeName = value; }
        }
        public PassType TrainingPassType
        {
            get { return _passType; }
            set { _passType = value; }
        }
        public DateTime IssueDate
        {
            get { return _issueDate; }
            set { _issueDate = value; }
        }
        public double FeePaid
        {
            get { return _feePaid; }
            set { _feePaid = value; }
        }
        public string IssuedBy
        {
            get { return _issuedBy; }
            set { _issuedBy = value; }
        }
        public int NumberOfClassesRemaining
        {
            get { return _numberOfClassesRemaining; }
            set { _numberOfClassesRemaining = value; }
        }
        public List<Member> MembersOnTrainingPass
        {
            get { return _members; }
            //set { _members = value; } //use the AddMembers() function
        }


        public Member Member
        {
            //only can set a new member, to get a member use ViewMembers() which returns the List of Members (_members)
            set{ AddMembers(value);}
        }

		public void AddPass(ref int TrainingPassID)
		{
            //Parameter: ref int TrainingPassID - when trainingpass ID is set, the int variable passed in will be updated in the calling function

            //           SqlTransaction - to keep the transaction as a whole unit, pass in an existing Transaction

            //Get the last ID in the DB
            string sqlSelectID = "SELECT TOP(1) ID FROM TrainingPass Order By ID DESC;";
            DataSet ds = Utilities.SearchDatabase(sqlSelectID);

            //check to see if it will be the first item inserted.
            if (ds.Tables[0].Rows.Count == 0)
            {
                TrainingPassID = 0;
            }
            else
            {
                TrainingPassID = int.Parse(ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()) + 1; // +1 to increment
            }

            //get the ID of PassType for the Training Pass
            PassType passType = TrainingPassType;
            string sqlPassTypeID = "SELECT * FROM PassType_Lookup WHERE Type='" + passType.PassTypeName + "';";
            DataSet passTypeDS = Utilities.SearchDatabase(sqlPassTypeID);
            int intPassTypeID = -1;
            intPassTypeID = int.Parse(passTypeDS.Tables[0].Rows[0].ItemArray.GetValue(0).ToString());


            //Construct INSERT statement
            string sqlInsert = "INSERT INTO TrainingPass VALUES ('" + TrainingPassID + "','" + PayeeName + "',CONVERT(datetime, '" + IssueDate + "', 103),'" + FeePaid + "','" + 
                IssuedBy + "','" + NumberOfClassesRemaining + "','" + intPassTypeID + "');";

            Utilities.DatabaseNonQuery(sqlInsert);
		}

		public void AddMembers(Member mem)
		{
            //add the passed in member to the _member List
            _members.Add(mem);			
		}
        public List<Member> ViewMembers()
        {
            //return the _member list
            return _members;
        }
	}
}
